﻿using CPPEI.WebApi;
using CPPEI.WebApiTool.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.WebApiTool
{
    class Program
    {
        public static string DocDirectory;
        public static string ApiUrl;

        public static Data.Settings s_settings = new Data.Settings();

        static void Main(string[] args)
        {
            Console.WriteLine("CPPEI WEB API Tool Ver:2.1.2");
            Console.WriteLine("生成接口说明文档");

            #region load config
            DocDirectory = System.Configuration.ConfigurationManager.AppSettings["DocDir"];
            Console.WriteLine(string.Format("DocDir:{0}", DocDirectory));
            if(DocDirectory.StartsWith("."))
            {
                DocDirectory = AppDomain.CurrentDomain.BaseDirectory + DocDirectory;
            }
            if (Directory.Exists(DocDirectory))
            {
                Directory.Delete(DocDirectory, true);
            }

            ApiUrl = System.Configuration.ConfigurationManager.AppSettings["ApiUrl"];
            Console.WriteLine(string.Format("ApiUrl:{0}", ApiUrl));
            #endregion

            DData data = Scan(AppDomain.CurrentDomain.BaseDirectory);

            //string filePath = string.Format("{0}\\WebApi.Config", WebApiDirectory);
            //CPPEI.WebApiTool.Writer.ConfigWriter.Write(filePath, data);


            WriteIndex(DocDirectory, data);

            WriteDocs(DocDirectory, data);

            //WriteTest(DocDirectory, data);

            //WriteCss(DocDirectory);

            Console.WriteLine("finished.");
            Console.ReadKey();
        }

        private static DData Scan(string directory)
        {
            DData data = new DData();

            string[] files = Directory.GetFiles(directory);
            foreach (var file in files)
            {
                DModule module = AssemblyUtil.Get(file);

                if (module != null)
                {
                    data.ModuleList.Add(module);
                }
            }

            data.Sort();

            return data;
        }

        private static void WriteIndex(string outputDirectory, DData data)
        {
            if(!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            string indexFilePath = string.Format("{0}\\index.html", outputDirectory);

            IndexWriter builder = new IndexWriter();

            builder.Begin();
            foreach (var module in data.ModuleList)
            {
                builder.Build(module);
            }
            builder.End();

            builder.Save(indexFilePath);
            Console.WriteLine(string.Format("Build Index Page:{0}", new FileInfo(indexFilePath).Name));
        }
     
        private static void WriteDocs(string outputDirectory, DData data)
        {
            string baseDirectory = string.Format("{0}\\docs\\", outputDirectory);

            if (!Directory.Exists(baseDirectory))
            {
                Directory.CreateDirectory(baseDirectory);
            }

            foreach (var module in data.ModuleList)
            {
                foreach (var scenario in module.ScenarioList)
                {
                    foreach (var action in scenario.Actions)
                    {
                        string filePath = string.Format("{0}\\{1}_{2}_{3}.html", baseDirectory,  module.Name, scenario.Name, action.Name);

                        DocWriter writer = new DocWriter(module, scenario, action);

                        writer.Build(filePath, ApiUrl);
                        Console.WriteLine(string.Format("Build Doc Page:{0}", new FileInfo(filePath).Name));
                    }
                }
            }
        }
        /*
     private static void WriteTest(string outputDirectory, DData data)
     {
         string baseDirectory = string.Format("{0}\\test\\", outputDirectory);

         if (!Directory.Exists(baseDirectory))
         {
             Directory.CreateDirectory(baseDirectory);
         }

         foreach (var scenario in data.ScenarioList)
         {
             foreach (var action in scenario.Actions)
             {
                 string filePath = string.Format("{0}\\{1}_{2}.html", baseDirectory, scenario.Name, action.Name);

                 TestPageWriter builder = new TestPageWriter(scenario, action);

                 builder.Build(filePath, ApiUrl);
                 Console.WriteLine(string.Format("Build Test Page:{0}", new FileInfo(filePath).Name));
             }
         }
     }
        
     private static void WriteCss(string outputDirectory)
     {
         string baseDirectory = string.Format("{0}\\css\\", outputDirectory);

         if (!Directory.Exists(baseDirectory))
         {
             Directory.CreateDirectory(baseDirectory);
         }

         string filePath = string.Format("{0}\\style.css", baseDirectory);
         string content = @"body {
 font-family:'Microsoft YaHei';
 font-size:0.8em;
}

.f_cmd {
 width:300px;
 background:#eae4e4;
}

.f_token {
 width:500px;
}

input {
 width:250px;
}

h1 {
 font-size:1.3em;
 color:#2d304b;
}

h2 {
 font-size:1.2em;
 color:#2d304b;
}

p {
 border:1px solid #11003c;
 padding:5px;
}

em {
 font-size:0.8em;
 color:#706d6d;
}

.doc_header {
 position:fixed; left:5px; top:5px; height:110px; right:5px;
}

.doc_body {
 overflow: scroll; position:fixed; left:5px; top:120px; bottom:5px; right:5px;
}

.def_table {
 width:100%;
 border-left:1px solid #11003c;
 border-collapse:collapse;
}
.def_table th {
 border-right:1px solid #11003c;
 border-top:1px solid #11003c;
 border-bottom:1px solid #11003c;
 background-color:#3d3f58;
 color:#FFF;
 width:300px;
}
.def_table .c_name {
 border-right:1px solid #11003c;
 border-top:1px solid #11003c;
 border-bottom:1px solid #11003c;
 width:150px;
}
.def_table .c_type {
 border-right:1px solid #11003c;
 border-top:1px solid #11003c;
 border-bottom:1px solid #11003c;
 width:100px;
}
.def_table .c_defaultvalue {
 border-right:1px solid #11003c;
 border-top:1px solid #11003c;
 border-bottom:1px solid #11003c;
 width:200px;
}
.def_table .c_description {
 border-right:1px solid #11003c;
 border-top:1px solid #11003c;
 border-bottom:1px solid #11003c;
 width:auto;
}
.def_table td {
 border-right:1px solid #11003c;
 border-bottom:1px solid #11003c;
}

.flag_new{
 color:red;
}

a{
 color:#020339;
}
a:active{
 color:#020339;
}";
         File.WriteAllText(filePath, content);
     }
      * */
    }
}
